package sprinklerbee.views;

import java.awt.Color;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import sprinklerbee.model.Configuration;
import sprinklerbee.model.Scheduler;
import sprinklerbee.model.SensorRealTime;
import sprinklerbee.model.Sprinkler;
import sprinklerbee.utils.MemoryCache;
import java.awt.Font;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class DisplayPanel extends JPanel {

	private static JLabel n_1;
	private static JLabel n_2;
	private static JLabel n_3;
	private static JLabel n_4;
	private static JLabel w_1;
	private static JLabel w_2;
	private static JLabel s_1;
	private static JLabel s_2;
	private static JLabel s_3;
	private static JLabel e_1;
	private static JLabel e_2;
	private static JLabel e_3;
	private static Icon onIcon;
	private static Icon offIcon;
	private static Icon disabledIcon;

	public DisplayPanel() {
		JLabel bg_display = new JLabel("");

		JPanel panel = new JPanel();
		panel.setBounds(12, 20, 700, 500);
		add(panel);
		panel.setLayout(null);

		// Group top
		JLabel group_north = new JLabel();
		group_north.setForeground(new Color(255, 165, 0));
		group_north.setBounds(341, 44, 101, 41);
		panel.add(group_north);
		group_north.setFont(new Font("Times New Roman", Font.BOLD, 36));
		group_north.setText("North");
		group_north.setBorder(null);

		JLabel sp_1N = new JLabel();
		sp_1N.setForeground(Color.BLUE);
		sp_1N.setBounds(164, 44, 50, 20);
		panel.add(sp_1N);
		sp_1N.setFont(new Font("Times New Roman", Font.BOLD, 16));
		sp_1N.setText("1N");

		n_1 = new JLabel();
		n_1.setBounds(174, 60, 18, 18);
		panel.add(n_1);

		JLabel sp_2N = new JLabel();
		sp_2N.setForeground(Color.BLUE);
		sp_2N.setBounds(418, 30, 50, 20);
		panel.add(sp_2N);
		sp_2N.setFont(new Font("Times New Roman", Font.BOLD, 16));
		sp_2N.setText("2N");

		n_2 = new JLabel("");
		n_2.setBounds(434, 44, 18, 18);
		panel.add(n_2);

		JLabel sp_3N = new JLabel();
		sp_3N.setForeground(Color.BLUE);
		sp_3N.setBounds(627, 34, 50, 20);
		panel.add(sp_3N);
		sp_3N.setFont(new Font("Times New Roman", Font.BOLD, 16));
		sp_3N.setText("3N");

		n_3 = new JLabel("");
		n_3.setBounds(640, 60, 18, 18);
		panel.add(n_3);

		JLabel sp_4N = new JLabel();
		sp_4N.setText("4N");
		sp_4N.setForeground(Color.BLUE);
		sp_4N.setFont(new Font("Times New Roman", Font.BOLD, 16));
		sp_4N.setBounds(40, 96, 50, 20);
		panel.add(sp_4N);

		n_4 = new JLabel("");
		n_4.setBounds(35, 115, 18, 18);
		panel.add(n_4);

		// Group right
		JLabel group_east = new JLabel();
		group_east.setForeground(new Color(255, 165, 0));
		group_east.setBounds(605, 246, 85, 37);
		panel.add(group_east);
		group_east.setText("East");
		group_east.setFont(new Font("Times New Roman", Font.BOLD, 36));
		group_east.setBorder(null);

		JLabel sp_1E = new JLabel();
		sp_1E.setForeground(Color.BLUE);
		sp_1E.setBounds(522, 165, 50, 20);
		panel.add(sp_1E);
		sp_1E.setFont(new Font("Times New Roman", Font.BOLD, 16));
		sp_1E.setText("1E");

		e_1 = new JLabel("");
		e_1.setBounds(522, 181, 18, 18);
		panel.add(e_1);

		JLabel sp_2E = new JLabel();
		sp_2E.setForeground(Color.BLUE);
		sp_2E.setBounds(627, 209, 50, 20);
		panel.add(sp_2E);
		sp_2E.setFont(new Font("Times New Roman", Font.BOLD, 16));
		sp_2E.setText("2E");

		e_2 = new JLabel("");
		e_2.setBounds(637, 225, 18, 18);
		panel.add(e_2);

		JLabel sp_3E = new JLabel();
		sp_3E.setForeground(Color.BLUE);
		sp_3E.setBounds(585, 324, 50, 20);
		panel.add(sp_3E);
		sp_3E.setFont(new Font("Times New Roman", Font.BOLD, 16));
		sp_3E.setText("3E");

		e_3 = new JLabel("");
		e_3.setBounds(605, 306, 18, 18);
		panel.add(e_3);

		// Group bottom
		JLabel group_south = new JLabel();
		group_south.setForeground(new Color(255, 165, 0));
		group_south.setBounds(256, 358, 94, 41);
		panel.add(group_south);
		group_south.setText("South");
		group_south.setFont(new Font("Times New Roman", Font.BOLD, 36));
		group_south.setBorder(null);

		JLabel sp_1S = new JLabel();
		sp_1S.setForeground(Color.BLUE);
		sp_1S.setBounds(30, 408, 50, 20);
		panel.add(sp_1S);
		sp_1S.setFont(new Font("Times New Roman", Font.BOLD, 16));
		sp_1S.setText("1S");

		s_1 = new JLabel("");
		s_1.setBounds(10, 408, 18, 18);
		panel.add(s_1);

		JLabel sp_2S = new JLabel();
		sp_2S.setForeground(Color.BLUE);
		sp_2S.setBounds(392, 358, 50, 20);
		panel.add(sp_2S);
		sp_2S.setFont(new Font("Times New Roman", Font.BOLD, 16));
		sp_2S.setText("2S");

		s_2 = new JLabel("");
		s_2.setBounds(369, 374, 18, 18);
		panel.add(s_2);

		JLabel sp_3S = new JLabel();
		sp_3S.setForeground(Color.BLUE);
		sp_3S.setBounds(608, 395, 50, 20);
		panel.add(sp_3S);
		sp_3S.setFont(new Font("Times New Roman", Font.BOLD, 16));
		sp_3S.setText("3S");

		s_3 = new JLabel("");
		s_3.setBounds(630, 400, 18, 18);
		panel.add(s_3);

		// Group left
		JLabel group_west = new JLabel();
		group_west.setForeground(new Color(255, 165, 0));
		group_west.setBounds(30, 179, 78, 46);
		panel.add(group_west);
		group_west.setFont(new Font("Times New Roman", Font.BOLD, 36));
		group_west.setText("West");
		group_west.setBorder(null);

		JLabel sp_1W = new JLabel();
		sp_1W.setForeground(Color.BLUE);
		sp_1W.setBounds(40, 233, 50, 20);
		panel.add(sp_1W);
		sp_1W.setFont(new Font("Times New Roman", Font.BOLD, 16));
		sp_1W.setText("1W");

		w_1 = new JLabel("");
		w_1.setBounds(40, 254, 18, 18);
		panel.add(w_1);

		JLabel sp_2W = new JLabel();
		sp_2W.setForeground(Color.BLUE);
		sp_2W.setBounds(160, 285, 50, 20);
		panel.add(sp_2W);
		sp_2W.setFont(new Font("Times New Roman", Font.BOLD, 16));
		sp_2W.setText("2W");

		w_2 = new JLabel("");
		w_2.setBounds(164, 300, 18, 18);
		panel.add(w_2);
		bg_display.setBounds(12, 20, 700, 500);
		bg_display.setIcon(new ImageIcon(getClass().getResource(
				"/icons/bg_display.jpg")));
		bg_display.setOpaque(true);
		add(bg_display);

		setLayout(null);
		setBorder(new TitledBorder("Display Panel"));
		setBackground(Color.GREEN);
		
		onIcon=new ImageIcon(getClass().getResource("/icons/on.png"));
		offIcon=new ImageIcon(getClass().getResource("/icons/off.png"));
		disabledIcon=new ImageIcon(getClass().getResource("/icons/disabled.png"));
		
		loadData();
	}

	public static void loadData() {
		SensorRealTime sensorRealTime = (SensorRealTime) MemoryCache.getMemoryCache().get("sensorRealTime");
		Map<String, Configuration> configurations=(Map<String, Configuration>) MemoryCache.getMemoryCache().get("configurations");
		List<Scheduler> schedulers =  (List<Scheduler>) MemoryCache.getMemoryCache().get("schedulersRealTime");
		Map<String, Sprinkler> disables= (Map<String, Sprinkler>) MemoryCache.getMemoryCache().get("disablesSprinklers");
		Map<String, JLabel> sprinklers = new HashMap<String, JLabel>();
		sprinklers.put("1N", n_1);
		sprinklers.put("2N", n_2);
		sprinklers.put("3N", n_3);
		sprinklers.put("4N", n_4);
		sprinklers.put("1W", w_1);
		sprinklers.put("2W", w_2);
		sprinklers.put("1S", s_1);
		sprinklers.put("2S", s_2);
		sprinklers.put("3S", s_3);
		sprinklers.put("1E", e_1);
		sprinklers.put("2E", e_2);
		sprinklers.put("3E", e_3);
		Set<String> keys=sprinklers.keySet();
		if (String.valueOf(sensorRealTime.getRain()).equals("0.0")){
			if (sensorRealTime.getTemperature()>Double.parseDouble(configurations.get("maxtemp").getValue())) {
				for (String key:keys) {
					if (null!=disables.get(key)) {
						sprinklers.get(key).setIcon(disabledIcon);
					}else{
						sprinklers.get(key).setIcon(onIcon);
					}
				}
			}else if (sensorRealTime.getTemperature()<Double.parseDouble(configurations.get("mintemp").getValue())) {
				for (String key:keys) {
					if (null!=disables.get(key)) {
						sprinklers.get(key).setIcon(disabledIcon);
					}else{
						sprinklers.get(key).setIcon(offIcon);
					}
				}
			}else{
				boolean flag=false;
				for (String key:keys) {
					flag=false;
					for (Scheduler scheduler : schedulers) {
						if (key.equals(scheduler.getSprinkler().getName())) {
							flag=true;
							break;
						}
					}
					if (null!=disables.get(key)) {
						sprinklers.get(key).setIcon(disabledIcon);
					}else{
						if (flag) {
							sprinklers.get(key).setIcon(onIcon);
						}else{
							sprinklers.get(key).setIcon(offIcon);
						}
					}
					
				}
			}
		}else{
			for (String key:keys) {
				if (null!=disables.get(key)) {
					sprinklers.get(key).setIcon(disabledIcon);
				}else{
					sprinklers.get(key).setIcon(offIcon);
				}
			}
		}
		
	}
}
